package com.epalmpay.controller.channel.analyze;

import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.analyze.CardAnalyzeInDTO;
import com.epalmpay.dto.analyze.CardAnalyzeOutDTO;
import com.epalmpay.dto.analyze.CardDataOutDTO;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.dataanalyze.ICardDataAnalyzeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value="/merchant/cardDataAnalyze")
public class CardAnalyzeController extends BaseController{

    @Autowired
    private ICardDataAnalyzeService cardDataAnalyzeService;

    /**
     * 查询本期的数据(月或周)
     * @param cardAnalyzeInDTO
     * @return
     */
    @RequestMapping(value="/dataAnalyze")
    @ResponseBody
    public CardAnalyzeOutDTO dataAnalyze(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardAnalyzeOutDTO result =new CardAnalyzeOutDTO();
        try {
            result= cardDataAnalyzeService.queryCardTongji(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    /**
     *查询上上周的数据
     * @param cardAnalyzeInDTO
     * @return
     */
    @RequestMapping(value="/naturalWeeks")
    @ResponseBody
    public CardAnalyzeOutDTO naturalWeeks(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardAnalyzeOutDTO result =new CardAnalyzeOutDTO();
        try {
            result = cardDataAnalyzeService.preBeforeSevenDaysCardData(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();
        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询当前月份的上一个月的数据
     * @param cardAnalyzeInDTO
     * @return
     */
    @RequestMapping(value="/dataAnalyzePre")
    @ResponseBody
    public CardAnalyzeOutDTO dataAnalyzePre(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardAnalyzeOutDTO result =new CardAnalyzeOutDTO();
        //查询上个月的数据
        int year = Integer.parseInt(cardAnalyzeInDTO.getDateAnalye().split("-")[0]);
        int month = Integer.parseInt(cardAnalyzeInDTO.getDateAnalye().split("-")[1]);

        int preYear;
        int preMonth;
        if(month==1||"1".equals(month)){   //如果是一月份    2018-01
            preYear=year-1;  //年份减一年
            preMonth=12;    //月份变为12月份
        }else{     // 否则只减月份
            preYear=year;
            preMonth=month-1;
        }
        cardAnalyzeInDTO.setDateAnalye(preYear+"-"+(preMonth<10?"0"+preMonth:preMonth));//上个月的日期
        try {
            result= cardDataAnalyzeService.queryCardTongji(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value="/dataAnalyzeTu")
    @ResponseBody
    public CardDataOutDTO dataAnalyz(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.cardAnalyzeTu(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    //本月核销 时间曲线图数据
    @RequestMapping(value="/queryMonthHours")
    @ResponseBody
    public CardDataOutDTO queryMonthHours(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.queryMonthHours(cardAnalyzeInDTO);
        } catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }
    //上月核销 时间曲线图数据
    @RequestMapping(value="/prequeryMonthHours")
    @ResponseBody
    public CardDataOutDTO prequeryMonthHours(CardAnalyzeInDTO cardAnalyzeInDTO){
        //查询上个月的数据
        int year = Integer.parseInt(cardAnalyzeInDTO.getDateAnalye().split("-")[0]);
        int month = Integer.parseInt(cardAnalyzeInDTO.getDateAnalye().split("-")[1]);

        int preYear;
        int preMonth;
        if(month==1||"1".equals(month)){   //如果是一月份    2018-01
            preYear=year-1;  //年份减一年
            preMonth=12;    //月份变为12月份
        }else{     // 否则只减月份
            preYear=year;
            preMonth=month-1;
        }
        cardAnalyzeInDTO.setDateAnalye(preYear+"-"+(preMonth<10?"0"+preMonth:preMonth));//上个月的日期
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.queryMonthHours(cardAnalyzeInDTO);
        } catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }
    @RequestMapping(value="/queryMonthHours2")
    @ResponseBody
    public CardDataOutDTO queryMonthHours2(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.queryMonthHours2(cardAnalyzeInDTO);
        } catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    /**
     *  //核销优惠券人群基础属性 统计男女比例
     * @param cardAnalyzeInDTO
     * @return
     */
    @RequestMapping(value="/menWomenProportion")
    @ResponseBody
    public CardAnalyzeOutDTO menWomenProportion(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardAnalyzeOutDTO result = new CardAnalyzeOutDTO();
        try {
            result=  cardDataAnalyzeService.menWomenProportion(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 统计每个年龄段的人数 占的比例
     * @param cardAnalyzeInDTO
     * @return
     */
    @RequestMapping(value="/queryAgeGroupBy")
    @ResponseBody
    public CardDataOutDTO queryAgeGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.queryAgeGroupBy(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value="/queryMembersTotal")
    @ResponseBody
    public CardAnalyzeOutDTO queryMembersTotal(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardAnalyzeOutDTO result = new CardAnalyzeOutDTO();
        try {
            result = cardDataAnalyzeService.queryMembersTotal(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }
    @RequestMapping(value="/queryCoupon")
    @ResponseBody
    public CardDataOutDTO queryCoupon(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.queryCoupon(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 统计每个金钱段 占的比例
     * @param cardAnalyzeInDTO
     * @return
     */
    @RequestMapping(value="/queryMoneyGroupBy")
    @ResponseBody
    public CardDataOutDTO queryMoneyGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.queryMoneyGroupBy(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 统计每个金钱段 占的比例
     * @param cardAnalyzeInDTO
     * @return
     */
    @RequestMapping(value="/queryIssueMoneyGroupBy")
    @ResponseBody
    public CardDataOutDTO queryIssueMoneyGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.queryIssueMoneyGroupBy(cardAnalyzeInDTO);

        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value="/querymenIssueWomen")
    @ResponseBody
    public CardAnalyzeOutDTO querymenIssueWomen(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardAnalyzeOutDTO result = new CardAnalyzeOutDTO();
        try {
            result=  cardDataAnalyzeService.querymenIssueWomen(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();
        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    /**
     *
     * @param cardAnalyzeInDTO
     * @return
     */
    @RequestMapping(value="/queryIssueAgeGroupBy")
    @ResponseBody
    public CardDataOutDTO queryIssueAgeGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        try {
            result = cardDataAnalyzeService.queryIssueAgeGroupBy(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value="/queryIssueMembersTotal")
    @ResponseBody
    public CardAnalyzeOutDTO queryIssueMembersTotal(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardAnalyzeOutDTO result = new CardAnalyzeOutDTO();
        try {
            result = cardDataAnalyzeService.queryIssueMembersTotal(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();

        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }
    @RequestMapping(value="/queryindustryCouponStatistical")
    @ResponseBody
    public CardDataOutDTO queryindustryCouponStatistical(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result = new CardDataOutDTO();
        cardAnalyzeInDTO.setQueryType(1); //根据行业分组显示数据
        try {
            result = cardDataAnalyzeService.queryindustryCouponStatistical(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result=null;
            ae.printStackTrace();
        }catch (Exception e) {
            result=null;
            e.printStackTrace();
        }
        return result;
    }
    @RequestMapping(value="/queryShopCouponStatistical")
    @ResponseBody
    public CardDataOutDTO queryShopCouponStatistical(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result2 = new CardDataOutDTO();
        cardAnalyzeInDTO.setQueryType(2); //根据行业分组显示数据
        try {
            result2 = cardDataAnalyzeService.queryindustryCouponStatistical(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result2=null;
            ae.printStackTrace();
        }catch (Exception e) {
            result2=null;
            e.printStackTrace();
        }
        return result2;
    }
    @RequestMapping(value="/queryCouponIssue")
    @ResponseBody
    public CardDataOutDTO queryCouponIssue(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result2 = new CardDataOutDTO();
        //cardAnalyzeInDTO.setQueryType(2); //根据行业分组显示数据
        try {
            result2 = cardDataAnalyzeService.queryCouponIssue(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result2=null;
            ae.printStackTrace();
        }catch (Exception e) {
            result2=null;
            e.printStackTrace();
        }
        return result2;
    }
    @RequestMapping(value="/queryCouponKxMoneyGroupBy")
    @ResponseBody
    public CardDataOutDTO queryCouponKxMoneyGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result2 = new CardDataOutDTO();
        try {
            cardAnalyzeInDTO.setQueryType(1);
            result2 = cardDataAnalyzeService.queryCouponKxMoneyGroupBy(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result2=null;
            ae.printStackTrace();
        }catch (Exception e) {
            result2=null;
            e.printStackTrace();
        }
        return result2;
    }

    @RequestMapping(value="/queryCouponIssueMoneyGroupBy")
    @ResponseBody
    public CardDataOutDTO queryCouponIssueMoneyGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result2 = new CardDataOutDTO();
        try {
            cardAnalyzeInDTO.setQueryType(2);
            result2 = cardDataAnalyzeService.queryCouponKxMoneyGroupBy(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result2=null;
            ae.printStackTrace();
        }catch (Exception e) {
            result2=null;
            e.printStackTrace();
        }
        return result2;
    }

    @RequestMapping(value="/queryTradeOffineExport")
    @ResponseBody
    public CardDataOutDTO queryTradeOffineExport(CardAnalyzeInDTO cardAnalyzeInDTO){
        CardDataOutDTO result2 = new CardDataOutDTO();
        try {
            result2 = cardDataAnalyzeService.queryTradeOffineExport(cardAnalyzeInDTO);
        } catch (ApplicationException ae) {
            result2=null;
            ae.printStackTrace();
        }catch (Exception e) {
            result2=null;
            e.printStackTrace();
        }
        return result2;
    }

}
